 /**
 * 逻辑、功能相关描述:
 *
 * @author 王浩羽 编写日期：Dec 29, 2008
 */
package com.ce.pms.system.occupation.service.impl;

import java.util.List;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.service.impl.AbstractCustomerEventServiceImpl;
import com.ce.pms.system.occupation.dao.IAbsentDocumentItemConfigDao;
import com.ce.pms.system.occupation.model.AbsentDocumentItemConfigDO;
import com.ce.pms.system.occupation.service.IAbsentDocumentItemConfigService;
import com.ce.pms.system.occupation.vo.AbsentDocumentItemConfigVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 入伙缺失资料设置DAO
 * @author 王浩羽
 *
 */
public class AbsentDocumentItemConfigServiceImpl extends AbstractCustomerEventServiceImpl<AbsentDocumentItemConfigVO, AbsentDocumentItemConfigDO> implements
	IAbsentDocumentItemConfigService
{
	private IAbsentDocumentItemConfigDao absentDocumentItemConfigDao;
	
	public boolean addAbsentDocumentItemConfig(AbsentDocumentItemConfigVO absentDocumentItemConfigVO)
	{
		if(!BlankUtil.isBlank(absentDocumentItemConfigVO) && !BlankUtil.isBlank(absentDocumentItemConfigVO.getAbsentDocumentItemName()))
		{
			if(isNameExisit(absentDocumentItemConfigVO.getAbsentDocumentItemName())){
				throw new BusinessException(getNameExisitExceptionInoKey(),BusinessException.WARNING);
			}
			AbsentDocumentItemConfigDO absentDocumentItemConfigDO = assembleV2D(absentDocumentItemConfigVO,null);
			absentDocumentItemConfigDO.setSysInfo(createInfo(absentDocumentItemConfigVO.getCreator()));
			return absentDocumentItemConfigDao.saveObject(absentDocumentItemConfigDO) != null;
		}
		return false;
	}
	
	
	public boolean deleteAbsentDocumentItemConfigById(Long absentDocumentItemConfigId)
	{
		if(absentDocumentItemConfigId==null)
		{
			return false;
		}
		try
		{
			absentDocumentItemConfigDao.deleteObjectById(absentDocumentItemConfigId);
			return true;
		}
		catch(RuntimeException e)
		{	//如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("occupation:system_delete_inused_exception",BusinessException.WARNING);
		}
	}
	
	public boolean editAbsentDocumentItemConfig(AbsentDocumentItemConfigVO absentDocumentItemConfigVO)
	{
		if(!BlankUtil.isBlank(absentDocumentItemConfigVO) && !BlankUtil.isBlank(absentDocumentItemConfigVO.getKeyword()))
		{
			AbsentDocumentItemConfigDO absentDocumentItemConfigDO = absentDocumentItemConfigDao.findById(absentDocumentItemConfigVO.getKeyword());
			if(absentDocumentItemConfigDO==null)
			{
				return false;
			}
			absentDocumentItemConfigDO.setAbsentDocumentItemName(absentDocumentItemConfigVO.getAbsentDocumentItemName());
			absentDocumentItemConfigDO.setDescription(absentDocumentItemConfigVO.getDescription());
			absentDocumentItemConfigDO.setSequence(absentDocumentItemConfigVO.getSequence());
			modifyInfo(absentDocumentItemConfigDO.getSysInfo(), absentDocumentItemConfigVO.getModifier());
			return absentDocumentItemConfigDao.updateObject(absentDocumentItemConfigDO) != null;
		}
		return false;
	}
	
	protected boolean isNameExisit(String contractorInfoName){
		return absentDocumentItemConfigDao.findUniqueByName(contractorInfoName)!=null;
	}
	
	public Page queryAbsentDocumentItemConfigListByPage(SearchVO searchVO)
	{
		return queryVOListByPage(searchVO);
	}
	
	
	
	public void setAbsentDocumentItemConfigDao(IAbsentDocumentItemConfigDao absentDocumentItemConfigDao)
	{
		this.absentDocumentItemConfigDao = absentDocumentItemConfigDao;
	}
	

	public List<AbsentDocumentItemConfigVO> queryAllAbsentDocumentItemConfig()
	{
		List<AbsentDocumentItemConfigDO> absentDocumentItemConfigDOList = absentDocumentItemConfigDao.findAll();
		sortList(absentDocumentItemConfigDOList);
		return assembleDOList2VOList(absentDocumentItemConfigDOList);
	}
	
	@Override
	protected int compareDO(AbsentDocumentItemConfigDO o1, AbsentDocumentItemConfigDO o2)
	{
		int s1 = Integer.MAX_VALUE;
		int s2 = Integer.MAX_VALUE;
		if(o1.getSequence() != null)
		{
			s1 = o1.getSequence();
		}
		if(o2.getSequence() != null)
		{
			s2 = o2.getSequence();
		}
		return s1 - s2;
	}

	@Override
	protected AbsentDocumentItemConfigDO findObjectDOById(Long id)
	{
		return absentDocumentItemConfigDao.findById(id);
	}


	@SuppressWarnings("unchecked")
	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{
		Page page = absentDocumentItemConfigDao.queryByPage(searchVO);
		List<AbsentDocumentItemConfigDO> list = page.getResult();
		sortList(list);
		page.setResult(list);
		return page;
	}
	
	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "occupation:system_name_exist_exception";
	}
}
